#include "Common.h"
#include "Console.h"

using namespace RayFrame;

IMPL_SINGLETON(Console);

Console::~Console()
{
  Clear();
}

void Console::Update( double timeElapsed )
{
  for(IterValue iter = m_values.begin(); iter != m_values.end(); iter++)
  {
    IConsoleValue* value = iter->second;
    value->Update();
  }
}

bool Console::Empty()
{
  return m_values.empty();
}

void Console::Clear()
{
  for(IterValue iter = m_values.begin(); iter != m_values.end(); iter++)
  {
    delete iter->second;
  }
  m_values.empty();
}

IConsoleValue* Console::FindValue( string name )
{
  IterValue iter = m_values.find(name);
  if(iter == m_values.end())
    return NULL;
  return iter->second;
}

static string m_parseErrorMsg;

bool ConsoleParser::Parse( std::string cmd )
{
  using namespace pystring;
  
  cmd = strip(lower(cmd));

  vector<string> symbols;
  split(cmd, symbols);

  string varName, varVal;
  if(symbols.size() == 3)
  {
     varName = symbols[0];
     varVal = symbols[2];
  }
  else
  {
    m_parseErrorMsg = "Cannot understand command.";
    return false;
  }

  IConsoleValue* value = Console::InstanceRef().FindValue(varName);
  if(!value)
  {
    m_parseErrorMsg = "Cannot find specified variable.";
    return false;
  }
  
  return true;
}
